package cn.deali.homework9.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.deali.homework9.MainApp;
import cn.deali.homework9.R;
import cn.deali.homework9.activity.NowPlayingActivity;
import cn.deali.homework9.model.LocalMusic;
import cn.deali.homework9.model.Music;
import cn.deali.homework9.utils.LocalMusicUtils;


public class LocalMusicAdapter extends RecyclerView.Adapter {
    List<LocalMusic> musics;
    Context context;

    public LocalMusicAdapter(Context context, List<LocalMusic> musics) {

        this.context = context;
        this.musics = musics;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.local_music_item, null);

        //返回ViewHolder实例
        return new PigViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        LocalMusic music = musics.get(position);
        //holder是RecyclerView.ViewHolder类型，是父类，不能直接获取到子类PigViewHolder的组件imageView...
        final PigViewHolder pigViewHolder = (PigViewHolder) holder;

        //渲染各组件
        pigViewHolder.imageView.setImageBitmap(LocalMusicUtils.getArtwork(
                context, music.getId(), music.getAlbumId(), true, true
        ));
        pigViewHolder.song_name.setText(music.getName());
        pigViewHolder.singer_name.setText(music.getSinger());

        MusicItemClick clickListener = new MusicItemClick(music);

        pigViewHolder.imageView.setOnClickListener(clickListener);
        pigViewHolder.singer_name.setOnClickListener(clickListener);
        pigViewHolder.song_name.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return musics.size();
    }

    class PigViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView song_name;
        TextView singer_name;


        public PigViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            song_name = itemView.findViewById(R.id.song_name);
            singer_name = itemView.findViewById(R.id.song_artist);
        }
    }

    class MusicItemClick implements View.OnClickListener {
        LocalMusic localMusic;

        MusicItemClick(LocalMusic localMusic) {
            this.localMusic = localMusic;
        }

        @Override
        public void onClick(View v) {
            // 替换当前播放列表
            String currentListName = MainApp.getInstance().getCurrentListName();
            if (!currentListName.equals("local")) {
                ArrayList<Music> playlist = new ArrayList<>();
                int musicIndex = 0;
                for (LocalMusic localMusic : musics) {
                    playlist.add(new Music(
                            musicIndex++,
                            localMusic.getName(),
                            LocalMusicUtils.getArtwork(
                                    context, localMusic.getId(), localMusic.getAlbumId(), true, true
                            ),
                            localMusic.getSinger(),
                            localMusic.getPath()
                    ));
                }
                MainApp.getInstance().setPlaylist(playlist);
            }

            int currentMusicIndex = 0;
            int musicIndex = 0;

            for (LocalMusic localMusic : musics) {
                if (this.localMusic.getId() == localMusic.getId()) {
                    currentMusicIndex = musicIndex;
                }
                musicIndex++;
            }

            // 设置当前播放的音乐，播放界面根据当前播放提取 歌曲名、歌手名、时间长度、专辑图片等
            MainApp.getInstance().setCurrentMusic(new Music(
                    currentMusicIndex,
                    localMusic.getName(),
                    LocalMusicUtils.getArtwork(context, localMusic.getId(), localMusic.getAlbumId(), true, true),
                    localMusic.getSinger(),
                    localMusic.getPath()
            ));

            // 设置当前列表名称
            MainApp.getInstance().setCurrentListName("local");

            Intent intent = new Intent(v.getContext(), NowPlayingActivity.class);   //跳转到播放页面
            context.startActivity(intent);
        }
    }
}
